package com.paw.common.context;

import cn.hutool.core.util.StrUtil;

/**
 * 用户信息上下文
 * @author Rubble
 * @date 2021/8/27
 **/
public class UserContextHolder {
  private static ThreadLocal<UserContext> local = ThreadLocal.withInitial(() -> new UserContext());


  public static void setUser(UserContext userContext){
    local.set(userContext);
  }

  public static UserContext getUser(){
    return local.get();
  }

  public static void remove(){
    local.remove();
  }

  public static boolean hasUserContext(){
    UserContext userContext = getUser();
    return userContext!=null && userContext.getId()!=null;
  }


  public static Long getUserId(){
    return getUser().getId();
  }

}
